import 'package:flutter/material.dart';
import 'package:yqmssa_app/views/landing/state.dart';

///会话Widget
class ChatWidget extends StatefulWidget {
  final ValueChanged<String> onTap;
  final ChatDM data;

  const ChatWidget({super.key, required this.onTap, required this.data});

  @override
  State<ChatWidget> createState() => _ChatWidgetState();
}

class _ChatWidgetState extends State<ChatWidget> {
  double opacity = 0;

  @override
  void initState() {
    super.initState();
    setState(() {
      opacity = 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedOpacity(
      opacity: opacity,
      duration: const Duration(seconds: 2),
      curve: Curves.decelerate,
      child: Container(
        margin: const EdgeInsets.only(top: 20),
        child: widget.data.userId == 0 ? waiter() : customer(),
      ),
    );
  }

  Widget waiter() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Image.asset("images/customer_service.png", fit: BoxFit.fill, width: 46),
        const SizedBox(width: 10),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: const EdgeInsets.all(12),
                decoration: const BoxDecoration(
                  color: Color(0XFFFBFBFB),
                  borderRadius: BorderRadius.only(
                    topRight: Radius.circular(8),
                    bottomLeft: Radius.circular(8),
                    bottomRight: Radius.circular(8),
                  ),
                ),
                child: RichText(
                  text: TextSpan(style: const TextStyle(color: Color(0XFF666666), fontSize: 15), children: [
                    TextSpan(text: widget.data.content),
                    WidgetSpan(
                        child: Visibility(
                            visible: widget.data.btnShow,
                            child: GestureDetector(
                                onTap: () {
                                  // Get.to(const PartnerContextPage());
                                },
                                child: const Text(
                                  "点击此处查看合作机构名单>>",
                                  style: TextStyle(color: Color(0XFF3B73FA), fontSize: 16),
                                ))))
                  ]),
                ),
              ),
              Wrap(
                direction: Axis.horizontal,
                children: List.generate(
                  widget.data.btnNames.length,
                  (index) => Container(
                    padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                    margin: const EdgeInsets.only(right: 10, top: 10),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(24),
                        border: Border.all(color: const Color(0XFFEFEFEF), width: 1),
                        color: widget.data.selValue == widget.data.btnNames[index] ? const Color(0XFF3B73FA) : Colors.white),
                    child: GestureDetector(
                      onTap: () {
                        setState(() {
                          widget.data.selValue = widget.data.btnNames[index];
                        });
                        widget.onTap.call(widget.data.btnNames[index]);
                      },
                      child: Text(
                        widget.data.btnNames[index],
                        style: TextStyle(
                            color: widget.data.selValue == widget.data.btnNames[index] ? Colors.white : Colors.black54,
                            fontSize: 14,
                            fontWeight: FontWeight.w500),
                      ),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
        const SizedBox(width: 30),
      ],
    );
  }

  Widget customer() {
    // final oneself = Get.find<OneselfLogic>();
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        const SizedBox(width: 30),
        Container(
          padding: const EdgeInsets.all(12),
          decoration: const BoxDecoration(
            color: Color(0XFF3B73FA),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8),
              bottomLeft: Radius.circular(8),
              bottomRight: Radius.circular(8),
            ),
          ),
          child: Text(
            widget.data.content,
            style: const TextStyle(color: Colors.white, fontSize: 16),
          ),
        ),
        const SizedBox(width: 10),
        // ClipRRect(borderRadius: BorderRadius.circular(50), child: CachedNetworkImage(imageUrl: oneself.data.value.image, width: 46, fit: BoxFit.fitWidth))
      ],
    );
  }
}
